<?php namespace App\Http\Controllers\Api;

use App\Example;
use App\Exceptions\NotFoundException;
use App\Exceptions\SecurityException;
use App\Facilitator;
use App\Msg;
use App\Org;
use App\Sys;
use App\User;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Input;
use Hash;

class AdminController extends ApiController
{
    public function setFacPass(Guard $guard){
        $facId = Input::get('facid');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Facilitator $facInfo */
        $facInfo = Facilitator::find($facId);
        if(!$facInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $facInfo->state = 2;
        $facInfo->save();

        return $this->buildResponse(trans('设置成功'),$facInfo);
    }

    public function setInvitePass(Guard $guard){
        $facId = Input::get('facid');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Facilitator $facInfo */
        $facInfo = Facilitator::find($facId);
        if(!$facInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $facInfo->t_state = 2;
        $facInfo->save();

        return $this->buildResponse(trans('设置成功'),$facInfo);
    }

    public function setOrgPass(Guard $guard){
        $orgId = Input::get('orgid');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Org $orgInfo */
        $orgInfo = Org::find($orgId);
        if(!$orgInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $orgInfo->state = 2;
        $orgInfo->save();

        return $this->buildResponse(trans('设置成功'),$orgInfo);
    }

    public function setExamPass(Guard $guard){
        $examId = Input::get('examid');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Example $examInfo */
        $examInfo = Example::find($examId);
        if(!$examInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $examInfo->state = 2;
        $examInfo->save();

        return $this->buildResponse(trans('设置成功'),$examInfo);
    }

    public function delFac(Guard $guard){
        $facId = Input::get('facid');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Facilitator $facInfo */
        $facInfo = Facilitator::find($facId);
        if(!$facInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $facInfo->delete();

        return $this->buildResponse(trans('删除成功'),[]);
    }

    public function delInvite(Guard $guard){
        $facId = Input::get('facid');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Facilitator $facInfo */
        $facInfo = Facilitator::find($facId);
        if(!$facInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $facInfo->delete();

        return $this->buildResponse(trans('删除成功'));
    }

    public function delOrg(Guard $guard){
        $orgId = Input::get('orgid');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Org $orgInfo */
        $orgInfo = Org::find($orgId);
        if(!$orgInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $orgInfo->delete();

        return $this->buildResponse(trans('删除成功'));
    }

    public function delExam(Guard $guard){
        $examId = Input::get('examid');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Example $examInfo */
        $examInfo = Example::find($examId);
        if(!$examInfo)
            throw new NotFoundException(NotFoundException::UserInfoNotFound);

        $examInfo->delete();

        return $this->buildResponse(trans('删除成功'));
    }

    public function facNoPass(Guard $guard){
        $facId = Input::get('facid');
        $msg   = Input::get('msg');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Facilitator $facInfo */
        $facInfo = Facilitator::find($facId);
        $facInfo->state = 3;
        $facInfo->save();

        $msgInfo = new Msg();
        $msgInfo->user_id = $facInfo->user_id;
        $msgInfo->system_msg = '【服务商基础信息】'.$msg;
        $msgInfo->save();

        return $this->buildResponse(trans('提交成功'));
    }

    public function inviteNoPass(Guard $guard){
        $facId = Input::get('facid');
        $msg   = Input::get('msg');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Facilitator $facInfo */
        $facInfo = Facilitator::find($facId);
        $facInfo->t_state = 3;
        $facInfo->save();

        $msgInfo = new Msg();
        $msgInfo->user_id = $facInfo->user_id;
        $msgInfo->system_msg = '【推广员信息】'.$msg;
        $msgInfo->save();

        return $this->buildResponse(trans('提交成功'));
    }

    public function orgNoPass(Guard $guard){
        $orgId = Input::get('orgid');
        $msg   = Input::get('msg');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Org $orgInfo */
        $orgInfo = Org::find($orgId);
        $orgInfo->state = 3;
        $orgInfo->save();

        $msgInfo = new Msg();
        $msgInfo->user_id = $orgInfo->fac->user_id;
        $msgInfo->system_msg = '【团队信息】'.$msg;
        $msgInfo->save();

        return $this->buildResponse(trans('提交成功'));
    }

    public function examNoPass(Guard $guard){
        $examId = Input::get('examid');
        $msg   = Input::get('msg');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Example $examInfo */
        $examInfo = Example::find($examId);
        $examInfo->state = 3;
        $examInfo->save();

        $msgInfo = new Msg();
        $msgInfo->user_id = $examInfo->org->fac->user_id;
        $msgInfo->system_msg = '【案例信息】'.$msg;
        $msgInfo->save();

        return $this->buildResponse(trans('提交成功'));
    }

    public function changePwd(Guard $guard){
        $userid = Input::get('userid');
        $newpwd = Input::get('newpwd');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var User $user */
        $user = User::find($userid);
        if(!$user)
            throw new NotFoundException(NotFoundException::UserNotFound);

        $user->password = bcrypt($newpwd);
        $user->save();

        return $this->buildResponse(trans('密码修改成功'), $user);
    }

    public function setSysInfo(Guard $guard){
        $sitename = Input::get('sitename');
        $keyword = Input::get('keyword');
        $description = Input::get('description');
        $beian = Input::get('beian');
        $company = Input::get('company');
        $modemsg = Input::get('modemsg');

        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        /** @var Sys $sysInfo */
        $sysInfo = Sys::find(1);

        $sysInfo->sitename = !is_null($sitename) ? $sitename : $sysInfo->sitename;
        $sysInfo->keyword  = !is_null($keyword) ? $keyword : $sysInfo->keyword;
        $sysInfo->description = !is_null($description) ? $description : $sysInfo->description;
        $sysInfo->beian = !is_null($beian) ? $beian : $sysInfo->beian;
        $sysInfo->companyname = !is_null($company) ? $company : $sysInfo->companyname;
        $sysInfo->modemsg = !is_null($modemsg) ? $modemsg : $sysInfo->modemsg;

        $sysInfo->save();

        return $this->buildResponse(trans('设置成功'));
    }
}